package com.rongke.damo.medicalrecord.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.biz.service.UserManager;
import com.rongke.commons.JsonResp;
import com.rongke.model.InspectionRecord;
import com.rongke.model.MedicalRecords;
import com.rongke.model.OperationRecord;
import com.rongke.model.User;
import com.rongke.model.dict.DictOperation;
import com.rongke.model.dict.DictOperationType;
import com.rongke.service.DictOperationService;
import com.rongke.service.DictOperationTypeService;
import com.rongke.service.OperationRecordService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

/**
 * 手术记录
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/damo/operationRecord")
@Transactional
public class DamoOperationRecordController {
    private static final Logger    LOGGER = Logger.getLogger(DamoOperationRecordController.class);

    @Autowired
    private OperationRecordService operationRecordService;

    @Autowired
    private UserManager            userManager;

    @Autowired
    private MedicalRecordManager   medicalRecordManager;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addOperationRecord(@RequestBody Map<String, String> jsonMap) {

        try {
            LOGGER.debug("addOperationRecord");

            String mobileNo = jsonMap.get("phone");
            if (StringUtil.isEmpty(mobileNo)) {
                return JsonResp.fa(null);
            }

            User user = userManager.selectOrAddUser(mobileNo);
            if (null == user) {
                return JsonResp.fa(null);
            }
            String userId = user.getUserId();

            MedicalRecords mrInfo = medicalRecordManager.selectOrAddMrByUserId(userId);
            if (null == mrInfo) {
                return JsonResp.fa(null);
            }
            long mrId = mrInfo.getId();

            OperationRecord operation = new OperationRecord();
            operation.setMedicalRecordId(mrId);
            operation.setUserId(userId);
            operation.setOperationId(jsonMap.get("operationId"));
            operation.setOperationName(jsonMap.get("operationName"));
            operation.setDate(jsonMap.get("date"));
            operation.setPicContent(jsonMap.get("picContent"));
            operation.setTagInfo(jsonMap.get("tagInfo"));
            operation.setTypeId(jsonMap.get("typeId"));
            operation.setTypeName(jsonMap.get("typeName"));

            operationRecordService.insert(operation);
            JSONObject json = new JSONObject();
            json.put("id", operation.getId());

            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("addOperationRecord failed", e);
            return JsonResp.fa(null);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateOperationRecord(@RequestBody OperationRecord operationRecord) {
        LOGGER.debug("updateOperationRecord");

        try {
            OperationRecord orInfo = new OperationRecord();
            orInfo.setId(operationRecord.getId());
            orInfo.setOperationId(operationRecord.getOperationId());
            orInfo.setOperationName(operationRecord.getOperationName());
            orInfo.setDate(operationRecord.getDate());
            orInfo.setPicContent(operationRecord.getPicContent());
            orInfo.setTagInfo(operationRecord.getTagInfo());
            orInfo.setTypeId(operationRecord.getTypeId());
            orInfo.setTypeName(operationRecord.getTypeName());

            operationRecordService.updateById(operationRecord);

            JSONObject json = new JSONObject();
            json.put("id", operationRecord.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("updateOperationRecord failed", e);
            return JsonResp.fa(null);

        }
    }

    /**
     * 根据ID删除检查记录
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public JsonResp delOperationRecordById(long id) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("delOperationRecordById, id=" + id);
        }

        try {
            operationRecordService.deleteById(id);

            JSONObject json = new JSONObject();
            json.put("id", id);
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("delOperationRecordById failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @根据id查找病历记录报告
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectOperationRecord(Long id) {
        LOGGER.debug("selectOperationRecord");

        try {
            OperationRecord operationRecord = operationRecordService.selectById(id);
            // convertIdToName(operationRecord);
            return JsonResp.ok(operationRecord);
        } catch (Exception e) {
            LOGGER.error("selectOperationRecord failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * 根据具体的病历，获取所有的医生诊断书
     * 
     * @param medicalRecordId
     * @return
     */
    @RequestMapping(value = "/selectAllByPhone", method = RequestMethod.GET)
    public JsonResp selectList(String userId) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("selectList, userId=" + userId);
        }

        try {
            MedicalRecords mr = medicalRecordManager.selectByUserId(userId);
            if (null == mr) {
                return JsonResp.ok();
            }

            long mrId = mr.getId();

            EntityWrapper<OperationRecord> ew = new EntityWrapper<OperationRecord>();
            ew.eq("medical_record_id", mrId);
            ew.orderBy("date");
            List<OperationRecord> result = operationRecordService.selectList(ew);
            // for (OperationRecord record : result) {
            // convertIdToName(record);
            // }
            return JsonResp.ok(result);
        } catch (Exception e) {
            LOGGER.error("selectList failed, userId=" + userId, e);
            return JsonResp.fa(null);
        }
    }
}
